	<?php
/*
* Copyright 2003 Mark O'Sullivan
* This file is part of Vanilla.
* Vanilla is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
* Vanilla is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Vanilla; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
* The latest source code for Vanilla is available at www.lussumo.com
* Contact Mark O'Sullivan at mark [at] lussumo [dot] com
*
* Description: The Head control is used to display all of the elements in the html head of the page.
*/


// Writes out the page head
class Head extends Control {
//	var $Libraries;			// array of js libraries already loaded
	var $Scripts;			// Script collection
	var $_Scripts;			// Script collection
	var $StyleSheets;		// Stylesheet collection
	var $_StyleSheets;		// Stylesheet collection
	var $Strings;			// String collection
	var $BodyId;			// identifier assigned to the body tag
	var $Meta;				// An associative array of meta tags/content to be added to the head.
	var $PackManager;
	
	
	/*$Position:
	 	Position in the list of script. You should try to the appropriate:
			100-199: for libraries that can't be packed (e.g. YUI hosted on yahoo servers);
			200-299: for libraries that can be packed (e.g. jQuery);
			300-399: for script that can be pack;
			400+: for script that can't be packed.
	*/	
	function AddScript($ScriptLocation, $ScriptRoot = '~', $Position = '350', $ToPack = 1, $ToMinify = 0) {
		if (!is_array($this->_Scripts)) $this->_Scripts = array();
		if ($ScriptRoot == '~') {
			$ScriptRoot = $this->Context->Configuration['WEB_ROOT'];
		} else {
			//the script need a relative path to the forum to merge it
			//If you specify you specify a web root it can't be merge
			$ToPack = 0;
		}
		$Location = $ScriptLocation;
		if ($ScriptRoot != '') $Location = ConcatenatePath($ScriptRoot, $ScriptLocation);
		
		$this->InsertItemAt($this->_Scripts,
			array('Location' => $Location, 'Path' =>  $ScriptLocation, 'ToPack' => $ToPack, 'ToMinify' => $ToMinify),
			$Position);
	}
	
	function AddStyleSheet($StyleSheetLocation, $Media = '', $Position = '100', $StyleRoot = '~', $ToPack = 1) {
		if (!is_array($this->_StyleSheets)) $this->_StyleSheets = array();	
		if ($StyleRoot == '~') {
			$StyleRoot = $this->Context->Configuration['WEB_ROOT'];
		} else {
			$ToPack = 0;
		}
		$Sheet = $StyleSheetLocation;
		if ($StyleRoot != '') $Sheet = ConcatenatePath($StyleRoot, $StyleSheetLocation);
		$Media = strtolower($Media);
		$this->InsertItemAt($this->_StyleSheets,
			array('Sheet' => $Sheet, 'Path' => $StyleSheetLocation, 'Media' => $Media, 'ToPack' => $ToPack),
			$Position);
	}
	
	function AddString($String) {
		if (!is_array($this->Strings)) $this->Strings = array();
		$this->Strings[] = $String;
	}
	
	function Clear() {
		$this->ClearStrings();
		$this->ClearStyleSheets();
		$this->ClearScripts();		
	}
	
	function ClearStrings() {
		$this->Strings = array();
	}
	
	function ClearStyleSheets() {
		$this->_StyleSheets = array();
	}
	
	function ClearScripts() {
		$this->_Scripts = array();
	}

	function Head(&$Context) {
		$this->Name = 'Head';
		$this->BodyId = '';
		$this->Control($Context);
		$this->Meta = array();
		$this->PackManager = $this->Context->ObjectFactory->NewContextObject($this->Context, 'PackManager');
	}
	
	function Render() {
		// First sort the stylesheets by key
		$this->_SortSheets();
		$this->_SortScripts();
		$this->CallDelegate('PreRender');
		include(ThemeFilePath($this->Context->Configuration, 'head.php'));
		$this->CallDelegate('PostRender');
   }
   
	function _SortScripts(){
		if (is_array($this->_Scripts)) {
	   		$Pack = new Pack($this->Context);
	   		$Pack->Extension = 'js';
	   		$this->Scripts = array();
	   		ksort($this->_Scripts);	
	   		if($this->Context->Configuration['PACKER_ON']){
		   		foreach ($this->_Scripts as $Script) {
		   			$url = '';
		   			if ($Script['ToPack']){
		   				$Pack->AddFile($Script['Path'], $Script['ToMinify']);
		   			} else {
		   				if ($url = $this->PackManager->GetPackUrl($Pack)){
		   					$this->Scripts[] = $url;
		   				}
		   				$Pack->files_to_merge = array();
		   				$this->Scripts[] = $Script['Location'];
		   			}
		   		}
		   		$url = '';
				if ($url = $this->PackManager->GetPackUrl($Pack)){
		   			$this->Scripts[] = $url;
		   		}
	   		} else {
	   			foreach ($this->_Scripts as $Script) {
	   				$this->Scripts[] = $Script['Location'];
	   			}
	   		}
		}
	}   	
   	
	function _SortSheets(){
   		if(is_array($this->_StyleSheets)){
			$Pack = $this->Context->ObjectFactory->NewContextObject($this->Context, 'Pack');
			$Pack->Extension = 'css';
			$this->StyleSheets = array();
			ksort($this->_StyleSheets);
			if($this->Context->Configuration['PACKER_ON']){ //check if we should pack the sheets
				foreach ($this->_StyleSheets as $Sheet) {
					$url = '';
					if($Sheet['ToPack']){ //check is this sheet will be packed with the others
						$Pack->AddFile($Sheet['Path'], $Sheet['Media']); // we just add it to the list of sheets to pack
					} else {
						if($url = $this->PackManager->GetPackUrl($Pack)){ // we pack the sheets that need to be packed
							$this->StyleSheets[] = array('Sheet' => $url,	'Media' => 'all');
						}
						$Pack->files_to_merge = array(); // start a need pack
						$Pack->files_to_merge_by_id = array();
						$this->StyleSheets[] = $Sheet; //add this sheet alone to the head.
					}
				}
				$url = '';
	   			if($url = $this->PackManager->GetPackUrl($Pack)){ // pack the remaining script that need to be pack
					$this->StyleSheets[] = array('Sheet' => $url,	'Media' => 'all');
				}
			} else { //We don't pack them
				foreach ($this->_StyleSheets as $Sheet) {
					$this->StyleSheets[] = $Sheet;
				}
			}
		}
   	}
}
?>